﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using System.Xml.Linq;
using WindowsFormsApplication.General;

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        int intResult1;
        public Form1()
        {
            InitializeComponent();
        }

        void DoIndependentWork()
        {
            label1.Text += "Working . . . . . . .\r\n";
        }


        async Task<int> TaskOfTResult_MethodAsync()
        {
            // You need to add a reference to System.Net.Http to declare client.
            HttpClient client = new HttpClient();

            // GetStringAsync returns a Task<string>. That means that when you await the 
            // task you'll get a string (urlContents).
            Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

            // You can do work here that doesn't rely on the string from GetStringAsync.
            DoIndependentWork();

            // The await operator suspends AccessTheWebAsync. 
            //  - AccessTheWebAsync can't continue until getStringTask is complete. 
            //  - Meanwhile, control returns to the caller of AccessTheWebAsync. 
            //  - Control resumes here when getStringTask is complete.  
            //  - The await operator then retrieves the string result from getStringTask. 
            string urlContents = await getStringTask;

            // The return statement specifies an integer result. 
            // Any methods that are awaiting AccessTheWebAsync retrieve the length value. 
            return urlContents.Length;

        }

        // Signature specifies Task
        async Task Task_MethodAsync()
        {
            // . . .
            // The method has no return statement.  
            // Calls to TaskOfTResult_MethodAsync
            Task<int> returnedTaskTResult = TaskOfTResult_MethodAsync();
            int intResult = await returnedTaskTResult;
            // or, in a single statement
            intResult1 = await TaskOfTResult_MethodAsync();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            await Task_MethodAsync();
            label1.Text = intResult1.ToString();
        }

        private void IQueryButton_Click(object sender, EventArgs e)
        {
            List<String> myList = new List<String>() { "a", "ab", "cd", "bd" };

            IEnumerable<String> query = from s in myList
                                        where s.StartsWith("a")
                                        select s;

            string outputString=string.Empty;

            foreach (String s in query)
            {
                outputString += s + "\n";
            }

            MessageBox.Show(outputString);
        }

        private void timePickupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TimeTest t = new TimeTest();
            t.ShowDialog();
            //t.Show();
        }

        private void XDocumentLinq_Click(object sender, EventArgs e)
        {
                    
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                notifyIcon1.Visible = true;
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = true;
                notifyIcon1.Visible = false;
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void interlockedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InterlockTest t = new InterlockTest();
            t.ShowDialog();
        }

        private void dataTableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LinqForm lfrom = new LinqForm();
            lfrom.ShowDialog();
        }

        private void dataTableMergeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NonDockableForm dtt = new NonDockableForm();
            dtt.ShowDialog();
        }

        private void objectAndChildToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ObjectTest objectTest = new ObjectTest();
            objectTest.ShowDialog();
        }

        private void bindingTestToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BindingTest bt = new BindingTest();
            bt.ShowDialog();
        }

        private void imageTestToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImageListTest im = new ImageListTest();
            im.ShowDialog();
        }

        private void referenceTestToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //ReferenceTest rt = new ReferenceTest();
            //rt.ShowDialog();
            NonDockableForm dtt = new NonDockableForm();
            dtt.ShowDialog();
        }
    }
}
